#include "data.hpp"

DataManager::DataManager()
{
    _backup_file=Config::GetConfigObj()->GetBackupFile();
    pthread_rwlock_init(&_rwlock,nullptr);
    InitLoad();
}

DataManager::~DataManager()
{
    pthread_rwlock_destroy(&_rwlock);
}

bool DataManager::Insert(const BackupInfo &info)
{
    cout<<"调用插入"<<endl;
    pthread_rwlock_wrlock(&_rwlock);
    _table[info.url]=info;
    cout<<__FILE__<<"-"<<__LINE__<<":"<<"Insert():"<<endl;
    pthread_rwlock_unlock(&_rwlock);
    Storage();
    return true;
}

bool DataManager::Updata(const BackupInfo &info)
{
    pthread_rwlock_wrlock(&_rwlock);
    _table[info.url]=info;
    pthread_rwlock_unlock(&_rwlock);
    Storage();
    return true;
}

bool DataManager::GetOneByURL(const string &url, BackupInfo *info)
{
     pthread_rwlock_wrlock(&_rwlock);
     auto it=_table.find(url);
     if(it==_table.end())
     {
        pthread_rwlock_unlock(&_rwlock);
        return false;
     }
     *info=it->second;
     pthread_rwlock_unlock(&_rwlock);
     return true;
}

bool DataManager::GetOneByRealPath(const string &realpath, BackupInfo *info)
{
    pthread_rwlock_wrlock(&_rwlock);
    auto it=_table.begin();
    for(;it!=_table.end();++it)
    {
        if(it->second.real_path==realpath)
        *info=it->second;
        pthread_rwlock_unlock(&_rwlock);
        return true;
    }
    pthread_rwlock_unlock(&_rwlock);
    return false;
}

bool DataManager::GetAll(vector<BackupInfo> *arry)
{
    pthread_rwlock_wrlock(&_rwlock);
    auto it=_table.begin();
    for(;it!=_table.end();++it)
    {
        arry->push_back(it->second);
    }
    pthread_rwlock_unlock(&_rwlock);
    return true;
}

bool DataManager::Storage()
{
    //获取所有数据
    vector<BackupInfo> arry;
    GetAll(&arry);
    //添加到Json::Value
    Json::Value root;
    for(int i=0;i<arry.size();i++)
    {
        Json::Value item;
        item["pack_flag"]=arry[i].pack_flag;
        item["fsize"]=(Json::Value::Int64)arry[i].fsize;
        item["atime"]=(Json::Value::Int64)arry[i].atiem;
        item["mtime"]=(Json::Value::Int64)arry[i].mtime;
        item["real_path"]=arry[i].real_path;
        item["pack_path"]=arry[i].pack_path;
        item["url"]=arry[i].url;

        root.append(item);
    }
    //将json序列化
    string body;
    cloudBackup::JsonUtil::Serialize(root,&body);
    //写文件
    cloudBackup::FileUtil fu(_backup_file);
    fu.SetContent(body);
    return true;
}

bool DataManager::InitLoad()
{
    //将数据文件中的数据读取出来
    cloudBackup::FileUtil fu(_backup_file);

    //第一次运行，没有任何数据。
    if(fu.FileSize()==0)
    {
        cout<<__FILE__<<"-"<<__LINE__<<":"<<"第一次运行，没有任何数据"<<endl;
        return true;
    }
    string body;
    fu.GetContent(&body);
    //反序列化
    Json::Value root;
    cloudBackup::JsonUtil::UnSerialize(body,&root);
    //将反序列化的数据添加到table中for(int i=0;i<root.size();i++)
    for(int i=0;i<root.size();i++)
    {
        BackupInfo info;
        info.pack_flag=root[i]["pack_flag"].asBool();
        info.fsize=root[i]["fsize"].asInt64();
        info.mtime=root[i]["mtime"].asInt64();
        info.atiem=root[i]["atime"].asInt64();
        info.real_path=root[i]["real_path"].asString();
        info.pack_path=root[i]["pack_path"].asString();
        info.url=root[i]["url"].asString();

        Insert(info);
    }
    return true;
}
